package com.sgcc.fj.pmos.e_fj_order.presenter;

import android.text.TextUtils;

import com.blankj.utilcode.util.ToastUtils;
import com.sgcc.fj.pmos.common.base.BasePresenter;
import com.sgcc.fj.pmos.common.bean.BaseResponse;
import com.sgcc.fj.pmos.common.http.HttpInterConst;
import com.sgcc.fj.pmos.common.http.HttpRequestUtils;
import com.sgcc.fj.pmos.common.http.MvcNetCallback;
import com.sgcc.fj.pmos.common.http.MvpNetCallback;
import com.sgcc.fj.pmos.common.http.UploadUtil;
import com.sgcc.fj.pmos.e_fj_order.bean.PackageOrdersBean;
import com.sgcc.fj.pmos.e_fj_order.bean.SupplementContractBean;
import com.sgcc.fj.pmos.e_fj_order.bean.SupplementContractFileUploadBean;
import com.sgcc.fj.pmos.e_fj_order.contract.AddSupplementContract;
import com.sgcc.fj.pmos.e_fj_order.contract.SupplementContract;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class AddSupplementContractPresenter extends
        BasePresenter<AddSupplementContract.View> implements AddSupplementContract.Presenter {


    @Override
    public void upload(String path) {

        HttpRequestUtils.getInstance().requestFile(new HashMap<>(),
                "file", HttpInterConst.SUPPLEMENT_CONTRACT_FILE_UPLOAD,
                new File(path), new MvpNetCallback<SupplementContractFileUploadBean>(getView(), true) {
                    @Override
                    public void onResponse(SupplementContractFileUploadBean response, int id) {
                        super.onResponse(response, id);
                        if (null != response) {
                            if (response.getStatus() == 0
                                    && null != response.getData() && response.getData().size() > 0) {
                                getView().uploadSuccess(response.getData().get(0));
                            } else {
                                if (!TextUtils.isEmpty(response.getMessage()))
                                    ToastUtils.showLong(response.getMessage());
                                getView().uploadFailed();
                            }
                        } else {
                            ToastUtils.showLong("附件上传失败");
                            getView().uploadFailed();
                        }

                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);

                    }
                });

    }


    @Override
    public void getPackageOrders(String relationId) {
        Map<String, Object> params = new HashMap<>();
        params.put("relationId", relationId);
        params.put("_t", System.currentTimeMillis());
        HttpRequestUtils.getInstance().request(params, HttpInterConst.ORDER_GETPACKAGEORDERS, new MvpNetCallback<PackageOrdersBean>(getView(), false) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
            }

            @Override
            public void onResponse(PackageOrdersBean response, int id) {
                super.onResponse(response, id);
                if (!isViewAttached()) {
                    return;
                }
                if (response != null) {
                    if ("200".equals(response.getCode())) {
                        if (null != response.getData())
                            getView().getPackageOrdersSuccess(response.getData());
                    } else {
                        if (TextUtils.isEmpty(response.getMsg())) {
                            ToastUtils.showShort(response.getMsg());
                        }
                    }
                }

            }
        });
    }

    @Override
    public void addSupplementContract(String fileId, String monthlyDifprice, String relationId) {

        Map<String, Object> params = new HashMap<>();
        params.put("_t", System.currentTimeMillis());
        params.put("fileId", fileId);//文件id
        params.put("monthlyDifprice", monthlyDifprice);//月份和补充价差值
        params.put("relationId", relationId);//订单ID
        HttpRequestUtils.getInstance().request(params, HttpInterConst.SAVE_SUPPLEMENT_CONTRACT,
                new MvpNetCallback<BaseResponse>(getView(), true) {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        super.onError(call, e, id);
                        // 失败回调 判断界面是否销毁
                        if (!isViewAttached()) {
                            return;
                        }
                        getView().addSupplementContractFailed();
                    }

                    @Override
                    public void onResponse(BaseResponse response, int id) {
                        super.onResponse(response, id);
                        if (!isViewAttached()) {
                            return;
                        }
                        if (null != response && response.isSuccess())
                            getView().addSupplementContractSuccess();
                        else {
                            ToastUtils.showLong(response.getMsg());
                        }
                    }
                });


    }
}